// 页面的数据
var list = []
import Mock from 'mockjs'
// import { config } from 'vue/types/umd'

import { param2Obj } from '../utils/urls'

for (var i = 0; i < 200; i++) {
    list.push({
        id: Mock.mock('@increment'),
        name: Mock.mock('@cname'),
        StaffNo: Mock.mock('@string("number", 8)'),
        rolesType: Mock.mock('@natural(0, 1)'),//0管理员  1街道负责人
        explainType: Mock.mock('@natural(0, 1)'),//0该账号拥有所有的权限 1请上传车位信息
        time: Mock.mock('@datetime()'),
        type: Mock.mock('@natural(0, 1)') //0是  1否
    })
}

export default {
    getHomeData: config => {
        // console.log('config', config);
        // console.log('config', param2Obj(config.url));
        // 过滤  page,pageSize做分页的关键
        //(page-1)*pageSize <= index < page * pageSize    0<=下标<9
        var { keyword, page, pageSize } = param2Obj(config.url)
        var res = list.filter(item => item.name.indexOf(keyword) != -1)
        list = list
        // 分页
        var reslimit = res.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize
        })
        return {
            code: 200,
            msg: "请求成功",
            data: reslimit, //数据
            total: list.length
        }
    },

    // 删除
    deleteData: config => {
        // console.log("config", config);
        var { id } = JSON.parse(config.body)
        var res = list.filter(item => item.id != id)
        list = res
        return {
            code: 200,
            msg: "删除成功",
            data: null
        }

    },
    // 批量删除
    deleteallData: config => {
        // var { ids } = JSON.parse(config.body)
        var { id } = JSON.parse(config.body)

        // id = JSON.parse(config.body).item
        // var res = list.filter(item => !this.item.id.includes(id))
        // var { ids } = JSON.parse(config.body)
        // var res = List.filter(item => !ids.includes(item.id))
        var res = list.filter((item => item.id != id))
        console.log("id-----", JSON.parse(config.body));
        list = res
        return {
            code: 200,
            msg: "批量删除成功",
            data: null
        }
    },

    // 添加
    addOneData: config => {
        const { name, user, type, explain, valid } = JSON.parse(config.body)
        list.push({
            id: Mock.mock('@increment'),
            name: name,
            StaffNo: user,
            rolesType: type,
            explainType: explain,
            time: Mock.mock('@datetime()'),
            type: valid
        })
        return {
            code: 200,
            msg: "添加成功",
            data: null
        }
    },

    // 编辑
    alterOneData: config => {
        const { name, type, user, valid, id, explain } = JSON.parse(config.body)
        list.forEach(item => {
            // console.log("item------2", item);
            // console.log("item------1", id);

            if (item.id == id) {
                item.name = name,
                    item.StaffNo = user,
                    item.rolesType = type,
                    item.explainType = explain,
                    item.type = valid

                return
            }
        })
        return {
            code: 200,
            msg: "修改成功",
            data: null
        }
    }
}